import cv2
import numpy as np


def createHogVectorFromImg(img):
    winSize = (8, 8)
    blockSize = (8, 8)
    blockStride = (8, 8)
    cellSize = (4, 4)
    nbins = 9

    # img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY,dstCn=img.shape[2])
    # ret, img = cv2.threshold(img, 100, 120, cv2.THRESH_OTSU)

    hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins)
    img = cv2.resize(img, (16, 20))
    vec = hog.compute(img=img, winStride=(8, 8), padding=(0, 0))
    DIMEN = len(vec)
    vec = vec.astype(np.float32).reshape((1, DIMEN))
    return vec


def createHogVectorFromPath(path):
    img = cv2.imread(path)
    return createHogVectorFromImg(img)


if __name__ == '__main__':
    vec = createHogVectorFromPath("/Users/wangzy/PycharmProjects/WebTest_Validate/demo/validatecode_dec/data/1561380615.215248.png")

    print(vec.shape)
    print(type(vec))
    print(len(vec))
    print(vec)
